package com.aynu.webshop.web.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.aynu.webshop.web.model.Address;
import com.aynu.webshop.web.model.Category;
import com.aynu.webshop.web.model.User;
import com.aynu.webshop.web.service.AddressService;

@Controller
@RequestMapping(value = "/address")
public class AddressController {
	@Resource
	private AddressService addressService;
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：根据Uid获取所有地址信息
	 */
	@RequestMapping(value = "/getAllAddress", method = RequestMethod.GET)
	public ModelAndView getAllCategory(HttpServletRequest request,Integer uid) {
		ModelAndView mv = new ModelAndView();
		User user = (User) request.getSession().getAttribute("user");
		List<Address> list = addressService.selectListByUid(user.getuId());
		mv.addObject("list", list);
		mv.setViewName("user/myAddress");//需要修改
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：添加地址信息
	 */
	@RequestMapping(value = "/addAddress", method = RequestMethod.POST)
	public ModelAndView addAddress(HttpServletRequest request,Address address) {
		User user = (User) request.getSession().getAttribute("user");
		ModelAndView mv = new ModelAndView("redirect:getAllAddress");
		if(address!=null){
			address.setuId(user.getuId());
			addressService.insert(address);
		}
	//	mv.setViewName("user/myAddress");//需要修改
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：修改地址信息
	 */
	@RequestMapping(value = "/editAddress", method = RequestMethod.POST)
	public ModelAndView editAddress(HttpServletRequest request,Address address) {
		ModelAndView mv = new ModelAndView();
		if(address!=null){
			addressService.update(address);
		}
		mv.setViewName("category/categoryDetail");//需要修改
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：删除地址信息
	 */
	@RequestMapping(value = "/deleteAddress", method = RequestMethod.GET)
	public ModelAndView deleteAddress(HttpServletRequest request,Integer adid) {
		ModelAndView mv = new ModelAndView("redirect:getAllAddress");
			addressService.delete(adid);
		return mv;
	}

}
